{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Álgebra lineal básica en Julia\n",
    "Autor: Andreas Noack Jensen (MIT) (http://www.econ.ku.dk/phdstudent/noack/)\n",
    "(con edición de Jane Herriman)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Julia's syntax is very similar to other languages but there are some important differences. Define a matrix of random normal variates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A = rand(1:4,3,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Definir un vector de unos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = fill(1.0, (3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notamos que $A$ tiene el tipo Array{Int64,2} pero $x$ tiene tipo Array{Int64,1}. Julia define los alias a Vector{Type}=Array{Type,1} y Matrix{Type}=Array{Type,2}.\n",
    "\n",
    "Muchas de las operaciones básicas son idénticas a otros lenguajes\n",
    "\n",
    "#### Multiplicación"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "b = A*x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Traspuestas\n",
    "Como en otros lenguajes, `A'` es la transpuesta conjugada mientras que `A.'` es sólo la traspuesta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Asym = A + A'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Multiplicación traspuesta\n",
    "Julia nos permite escribir esto sin *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Apd = A'A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Resolviendo sistemas lineales\n",
    "El problema $Ax=b$ para $A$ cuadrada se resulve con la función \\."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A\\b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Sistemas sobredeterminados\n",
    "Cuando nuestra matriz es alta (número de renglones mayores al número de columnas), tenemos un sistema lineal sobredeterminado.\n",
    "\n",
    "\n",
    "En este caso \\ calcula la de mínimos cuadrados"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Atall = rand(3, 2)\n",
    "display(Atall)\n",
    "Atall\\b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "La función \\ también sirve ocn problemas deficientes de rango de mínimos cuadrados. En este caso, la solución no es única y Julia regresa el valor con la menor norma.\n",
    "\n",
    "Para crear un problema de rango deficiente de mínimos cuadrados, vamos a crear una matriz deficiente en rango con columnas linealmente dependientes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "v = randn(3)\n",
    "rankdef = [v v]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rankdef\\b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Sistemas indeterminados\n",
    "cuando A es corta (número de columnas mayor al número de renglones), tenemos un sistema indeterminado\n",
    "\n",
    "En este caso \\ regresa la solución con la norma mínima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Ashort = rand(2, 3)\n",
    "display(Ashort)\n",
    "Ashort\\b[1:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ejercicios\n",
    "\n",
    "\n",
    "\n",
    "```\n",
    "A = [\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " ]\n",
    "```\n",
    "\n",
    "Quieres obtener\n",
    "\n",
    "```\n",
    "A = [\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " ]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.2 Toma el producto de un vector `v` con sí mismo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.3 Toma el producto de un vector `v` con sí mismo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.1",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language": "Julia",
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
